package de.slothsoft.commons.swing.util;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Enumeration;

import javax.swing.DefaultListSelectionModel;
import javax.swing.table.TableColumn;

import de.slothsoft.commons.swing.treetable.JTreeTable;
import de.slothsoft.commons.swing.treetable.TreeTableModel;

public class JTreeTableUtil {

	// TODO: check javadoc

	public static final String COMMAND_MOUSE_CLICK = "mouseClick";
	public static final String COMMAND_KEY_PRESSED = "keyPressed";

	/**
	 * Creates a TreeTable with a model. Uses the
	 * <code>DisplayableTreeTableCellRenderer</code> on default.
	 * 
	 * @param model
	 *            - a model
	 * @return the newly created TreeTable
	 */

	public static JTreeTable createTreeTable(TreeTableModel model) {
		return createTreeTable(model, null);
	}

	/**
	 * Creates a TreeTable with a model and specific widths for it's columns.
	 * Uses the <code>DisplayableTreeTableCellRenderer</code> on default.
	 * 
	 * @param model
	 *            - a model
	 * @param columnWidths
	 *            - the columns' widths
	 * @return the newly created TreeTable
	 */

	public static JTreeTable createTreeTable(TreeTableModel model,
			int[] columnWidths) {

		JTreeTable TreeTable = new JTreeTable(model);
		TreeTable.setAutoResizeMode(JTreeTable.AUTO_RESIZE_OFF);
		createRenderer(TreeTable, columnWidths);
		TreeTable.setAutoResizeMode(JTreeTable.AUTO_RESIZE_ALL_COLUMNS);
		TreeTable.setAutoResizeMode(JTreeTable.AUTO_RESIZE_NEXT_COLUMN);
		TreeTable.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);

		return TreeTable;
	}

	/**
	 * Creates a renderer for each column of the TreeTable. Uses the
	 * <code>DisplayableTreeTableCellRenderer</code> on default.
	 * 
	 * @param TreeTable
	 *            - the TreeTable
	 */

	public static void createRenderer(JTreeTable TreeTable) {
		createRenderer(TreeTable, null);
	}

	/**
	 * Creates a renderer for each column of the TreeTable and sets the width of
	 * each TreeTable. Uses the <code>DisplayableTreeTableCellRenderer</code> on
	 * default.
	 * 
	 * @param TreeTable
	 *            - the TreeTable
	 * @param columnWidths
	 *            - the columns' widths
	 */

	public static void createRenderer(JTreeTable TreeTable, int[] columnWidths) {
		Enumeration<TableColumn> columns = TreeTable.getColumnModel()
				.getColumns();
		int columnNumber = 0;
		while (columns.hasMoreElements()) {
			TableColumn column = columns.nextElement();
			if (columnWidths != null) {
				setFixedColumnSize(column, columnWidths[columnNumber]);
			}

			column.setMinWidth(0);
			column.setMaxWidth(1000);
			columnNumber++;
		}
	}

	/**
	 * Sets the size of a column to a fixed, not changeable value
	 * 
	 * @param column
	 *            - the column
	 * @param columnSize
	 *            - the size of the column
	 */

	public static void setFixedColumnSize(TableColumn column, int columnSize) {
		column.setWidth(columnSize);
		column.setMinWidth(columnSize);
		column.setMaxWidth(columnSize);
	}

	/**
	 * Adds a selection listener to a TreeTable - e.g. a mouse listener and a
	 * key listener.
	 * 
	 * @param TreeTable
	 *            - the TreeTable to be added to
	 * @param listener
	 *            - the listener to be added
	 */

	public static void addSelectionListener(final JTreeTable TreeTable,
			final ActionListener listener) {
		if (listener == null) {
			return;
		}
		TreeTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				ActionEvent event = new ActionEvent(e.getSource(), e.getID(),
						COMMAND_MOUSE_CLICK);
				listener.actionPerformed(event);
			}
		});
		TreeTable.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				ActionEvent event = new ActionEvent(e.getSource(), e.getID(),
						COMMAND_KEY_PRESSED);
				listener.actionPerformed(event);
			}
		});
	}

	// TODO: this method
	//
	// /**
	// * Returns the selected element of a JTreeTable.
	// *
	// * @return one element or null
	// */
	//
	// public static <T> T getSelectedElement(JTreeTable TreeTable,
	// AbstractListTreeTableModel<T> model) {
	// int selected = TreeTable.getSelectedRow();
	// if (selected != -1) {
	// int modelIndex = TreeTable.getRowSorter().convertRowIndexToModel(
	// selected);
	// return model.getList().get(modelIndex);
	// }
	// return null;
	// }

	// TODO: this method

	// /**
	// * Returns the selected elements of a JTreeTable.
	// *
	// * @return a possible empty list
	// */
	//
	// public static <T> List<T> getSelectedElements(JTreeTable TreeTable,
	// AbstractTreeTableModel model) {
	// List<T> result = new ArrayList<T>(TreeTable.getSelectedRowCount());
	// int[] selected = TreeTable.getSelectedRows();
	// for (int s : selected) {
	// int modelIndex = TreeTable.getRowSorter().convertRowIndexToModel(s);
	// result.add(model.getList().get(modelIndex));
	// }
	// return result;
	// }

	private JTreeTableUtil() {
		// RUN YOU FOOLS!!!
	}
}
